  <!--
* description: 展车配置/展车标识解除
* author: wangjuan
* createdDate: 2020-11-16
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="setKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-table-cols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="false"
      :dynamic-is-show-more-btn="false"
    />

    <edit
      :key="editPopupsKey"
      :setting-data="settingData"
      :popups-visible="editPopupsVisible"
      :popups-state="editPopupsState"
      @close="close"
    />
    <carActivate
      :key="carActivatePopupsKey"
      :edit-data="editData"
      :popups-visible="carActivatePopupsVisible"
      :batch-row-data="carActivateRowData"
      :popups-state="editPopupsState"
      @closecarActivate="closecarActivate"
    />

  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import Vue from 'vue'
import carActivate from './carActivate.vue'
import api from '@/api/belowInfo'

export default {
  name: 'BelowConfig',
  components: {
    OneTableTemplate,
    Edit: () => import('./edit'),
    carActivate
  },

  // 组件混入对象：{data[listQuery] methods[queryTable]}
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      settingData: {},
      editData: {},
      editPopupsKey: 'editPopupsKey',
      setKey: 'setKey',
      carActivatePopupsVisible: false,
      carActivateRowData: {},
      carActivatePopupsKey: 'carActivatePopupsKey',
      // 网格查询API配置对象
      apiConfig: api.queryExhibitionCarSetting,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: '批次编号：',
              codeField: 'transferOrderCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '订单编号：',
              codeField: 'saleOrderCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '交付店：',
              codeField: 'deliveryDlrId',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMul: false,
              isMust: true,
              oFields: 'deliveryDlrId,deliveryDlrFullName',
              customObj: {
                api: api.ListOfDeliveryStoreContent
              }
            },
            {
              compKey: 'compKey4',
              labelName: '车系：',
              codeField: 'carSeriesId',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMul: false,
              isMust: true,
              oFields: 'carSeriesId,carSeriesCn',
              customObj: {
                api: api.listOfCarSeries
              }
            },
            {
              compKey: 'compKey5',
              labelName: '基础车型：',
              codeField: 'carConfigId',
              textField: 'carConfigCn',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMul: false,
              isMust: false,
              oFields: 'carConfigId,carConfigCn',
              parentFileds: 'value:carSeriesId',
              customObj: {
                api: api.queryCarType,
                listQuery: {
                  carSeriesId: ''
                }
              }
            },
            {
              compKey: 'compKey6',
              labelName: '选装包：',
              codeField: 'optionalPagId',
              textField: 'optionalPagName',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMul: false,
              isMust: false,
              oFields: 'optionalPagId,optionalPagName',
              parentFileds: 'value:carConfigId|carSeriesId',
              customObj: {
                api: api.queryCarOptionalPagLinkMessage,
                listQuery: {
                  carConfigId: '', carSeriesId: ''
                }
              }
            },
            {
              compKey: 'compKey7',
              labelName: '车身颜色：',
              codeField: 'carColorId',
              textField: 'carColorName',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'carColorId,carColorName',
              type: 'dropdownList',
              isMul: false,
              isMust: false,
              parentFileds: 'value:carConfigId|carSeriesId|optionalPagId',
              customObj: {
                listQuery: {
                  carConfigId: '', carSeriesId: '', optionalPagId: ''
                },
                api: api.queryCarColorLinkMessage
              }
            },
            {
              compKey: 'compKey8',
              labelName: '内饰颜色：',
              codeField: 'carIncolorId',
              textField: 'carIncolorName',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'carIncolorId,carIncolorName',
              type: 'dropdownList',
              isMul: false,
              isMust: false,
              parentFileds: 'value:carConfigId|carSeriesId|optionalPagId',
              customObj: {
                listQuery: {
                  carConfigId: '', carSeriesId: '', optionalPagId: ''
                },
                api: api.queryCarInColorLinkMessage
              }
            },
            {
              compKey: 'compKey9',
              labelName: 'VIN码：',
              codeField: 'VIN',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey10',
              labelName: '订单状态：',
              codeField: 'orderStatus',
              lookuptype: 'VE1003',
              component: () => import('@/components/org/LookupValue'),
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey11',
              labelName: '入库日期：',
              codeField: 'firstInDlrstockDate',
              component: () => import('@/components/org/datePicker/datePicker'),
              type: 'datePicker',
              isMul: false,
              isMust: false
            }
          ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'transferOrderDId',
              label: '订单子表id',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlIdOd',
              label: '并发字段id',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'controlBtn1',
              label: '操作',
              width: 180,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '设置展车',
                  rowFileds: 'show:signatoryname-null,show:signatoryphone-null,show:email-null',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.editCar(index)
                },
                {
                  compKey: 'propKey2',
                  labelName: '解除展车',
                  rowFileds: 'show:signatoryname--null,show:signatoryphone--null,show:email--null',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.remove(index)
                }
              ]
            },
            {
              prop: 'cheGuanPlatformBtn',
              label: '车管平台',
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '激活',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.carActivate(index)
                }
              ],
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'transferOrderCode',
              label: '批次编号',
              width: 140
            },
            {
              prop: 'saleOrderCode',
              label: '订单编号',
              width: 140
            },
            {
              prop: 'dlrShortName',
              label: '交付店',
              width: 140
            },
            {
              prop: 'exCarType',
              label: '是否展车'
            },
            {
              prop: 'signatoryname',
              label: '展车车主'
            },
            {
              prop: 'signatoryphone',
              label: '车主电话',
              width: 120
            },
            {
              prop: 'email',
              label: '车主邮箱',
              width: 140
            },
            {
              prop: 'VIN',
              label: 'VIN码',
              width: 140
            },
            {
              prop: 'orderStatus',
              label: '订单状态',
              width: 120
            },
            {
              prop: 'carSeriesCn',
              label: '车系'
            },
            {
              prop: 'carConfigCn',
              label: '基础车型'
            },
            {
              prop: 'optionalPagName',
              label: '选装包'
            },
            {
              prop: 'carColorName',
              label: '车身颜色'
            },
            {
              prop: 'carIncolorName',
              label: '内饰颜色'
            },
            {
              prop: 'firstInDlrstockDate',
              label: '入库日期',
              width: 140
            },
            {
              prop: 'carHouseName',
              label: '入库仓库'
            }
          ],

      // 表单数据
      formField: this.$utils.getFormField(this, {
        transferOrderCode: '',
        saleOrderCode: '',
        deliveryDlrId: '',
        carSeriesId: '',
        carConfigId: '',
        optionalPagId: '',
        carColorId: '',
        carIncolorId: '',
        VIN: '',
        orderStatus: '',
        firstInDlrstockDate: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    }
  },

  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    // 设置展车
    editCar(index) {
      this.settingData = this.$refs.multipleTable.list[index]
      this.editPopupsVisible = true
      this.editPopupsKey = this.$utils.generateId()
    },

    // 激活
    carActivate(index) {
      this.editData = this.$refs.multipleTable.list[index]
      this.carActivatePopupsVisible = true
      this.carActivatePopupsKey = this.$utils.generateId()
    },

    // 关闭弹窗
    closecarActivate(type) {
      if (type === 1) {
        this.queryTable(1)
      }
      this.carActivatePopupsVisible = false
      this.carActivatePopupsKey = this.$utils.generateId()
    },

    // 解除展车
    remove(index) {
      // // 先读取参数中的传输对象
      this.$confirm('确定要解除展车吗?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const returnObj = {
          // api配置
          apiConfig: api.deleteExhibitionCarSetting,
          // 条件/实体参数（变量），根据typeParam中的定义配置
          variables: {
            saleOrderDId: this.$refs.multipleTable.list[index].saleOrderDId,
            updateControlIdOd: this.$refs.multipleTable.list[index].updateControlIdOd,
            updateControlIdS: this.$refs.multipleTable.list[index].updateControlIdS
          }
        }
        // 转换了中台请求格式数据
        var paramD = this.$getParams(returnObj, null, this.dynamicApiStyle)
        // 调用中台API方法（可复用）
        this.$requestAPI(paramD, this.dynamicApiStyle).then(response => {
          if (response.result === '0') {
            this.$message({
              message: response.msg
            })
          } else {
            this.$message({
              message: response.msg
            })
          }
          this.queryTable(1)
        }).catch(_ => {})
      }).catch(() => {})
    }

  }

}
</script>
